package com.jwdf.uda.ui.fragment.user.login;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentLocalAuthBinding;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.ForgotPasswordStep1Action;
import com.jwdf.uda.ui.activity.ForgetPasswordActivity;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/7/13.
 */
public class LocalAuthCodeFragment extends BaseFragment {
    private FragmentLocalAuthBinding mDataBinding;
    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (R.id.tv_random_code == v.getId()) {
                mDataBinding.tvRandomCode.setText(UdaUtils.getRandomNumer());
            } else if (R.id.tv_random == v.getId()) {
                mDataBinding.tvRandomCode.setText(UdaUtils.getRandomNumer());
            } else if (R.id.btn_verify_local == v.getId()) {
                String mobile = mDataBinding.inputMobileEdit.getText().toString();
                String inputCode = mDataBinding.inputCodeEdit.getText().toString();
                String correctCode = mDataBinding.tvRandomCode.getText().toString();
                if (TextUtils.isEmpty(mobile)) {
                    ToastUtils.show(getContext(), getString(R.string.mobile_can_not_be_null));
                    return;
                }
                if (!UdaUtils.isMobile(mobile)) {
                    ToastUtils.show(getContext(), getString(R.string.please_correct_the_mobile));
                    return;
                }

                if (TextUtils.isEmpty(inputCode)) {
                    ToastUtils.show(getContext(), getString(R.string.code_can_not_be_null));
                    return;
                }

                if (!TextUtils.equals(inputCode, correctCode)) {
                    ToastUtils.show(getContext(), getString(R.string.code_error));
                    return;
                }
                authMobieCode(mobile);
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentLocalAuthBinding.inflate(inflater, container, false);
        initViews();
        return mDataBinding.getRoot();
    }
    private void initViews() {
        mDataBinding.tvRandom.setOnClickListener(mClickListener);
        mDataBinding.tvRandomCode.setOnClickListener(mClickListener);
        mDataBinding.btnVerifyLocal.setOnClickListener(mClickListener);
        mDataBinding.tvRandomCode.setText(UdaUtils.getRandomNumer());
        mDataBinding.tvRandom.setPaintFlags(mDataBinding.tvRandom.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        mDataBinding.tvRandom.setMovementMethod(LinkMovementMethod.getInstance());
        String mobile = getArguments().getString("mobile");
    }
    private void authMobieCode(final String mobile) {
        onNewRequestCall(ForgotPasswordStep1Action.newInstance(getContext(), mobile).request(new AHttpService.IResCallback<PbBean.PbResUserForgotPasswordStep1>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserForgotPasswordStep1> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(getView() != null) {
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
//                    UserEvent event = new UserEvent();
//                    event.setEvent(UserEvent.EVENT_STEP_1);
//                    event.setExtra(mobile);
//                    EventBusUtil.post(event);
                    getActivity().finish();
                    ForgetPasswordActivity.start(getContext(),"忘记密码",mobile);
                }
            }
        }));
    }
}
